package com.by.yz.custom;

/**
 * Created by mrs yang on 2016/10/11.
 */

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.by.yz.R;

public class CommonTitleBar extends RelativeLayout {
    /**
     * 左边返回图标<和文字说明的LinearLayout
     */
    private LinearLayout title_bar_layout_left;
    /**
     * 中间标题RelativeLayout布局
     */
    private RelativeLayout title_bar_layout_center;
    /**
     * 右边文字、图标RelativeLayout布局
     */
    private LinearLayout title_bar_layout_right;
    /**
     * 左边返回图标<
     */
    private ImageView title_bar_left_imageview;
    /**
     * 右边ImageView
     */
    private ImageView title_bar_right_iamgeview;
    /**
     * 左边返回文字说明
     */
    private TextView title_bar_left_textview;
    /**
     * 右边TextView，可点击
     */
    private TextView title_bar_right_textview;
    /**
     * 中间标题
     */
    private TextView title_bar_title;
    private Context mContext;

    public CommonTitleBar(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public CommonTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public CommonTitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        // 布局初始化
        LayoutInflater.from(mContext).inflate(R.layout.common_title_bar, this);
        if (isInEditMode()) {
            return;
        }
        title_bar_layout_left = (LinearLayout) findViewById(
                R.id.title_bar_layout_left);
        title_bar_layout_center = (RelativeLayout) findViewById(
                R.id.title_bar_layout_center);
        title_bar_layout_right = (LinearLayout) findViewById(
                R.id.title_bar_layout_right);
        // 左边布局包含的组件初始化
        title_bar_left_imageview = (ImageView) findViewById(
                R.id.title_bar_left_imageview);
        title_bar_left_textview = (TextView) findViewById(
                R.id.title_bar_left_textview);
        // 中间布局包含的组件初始化
        title_bar_title = (TextView) findViewById(R.id.title_bar_title);
        // 右边布局包含的组件初始化
        title_bar_right_iamgeview = (ImageView) findViewById(
                R.id.title_bar_right_iamgeview);
        title_bar_right_textview = (TextView) findViewById(
                R.id.title_bar_right_textview);
    }

    /**
     *
     * @Title setLayoutVisiblity
     * @Description 布局的显示和隐藏
     * @param LeftVisibility
     *            左边布局
     * @param centerVisibility
     *            中间布局
     * @param rightVisibility
     *            右边布局
     * @return void
     * @throws
     *
     * 			<pre>
     * View.VISIBLE,View.VISIBLE,View.GONE
     *             </pre>
     */
    public void setLayoutVisiblity(int LeftVisibility, int centerVisibility,
                                   int rightVisibility) {
        title_bar_layout_left.setVisibility(LeftVisibility);
        title_bar_layout_center.setVisibility(centerVisibility);
        title_bar_layout_right.setVisibility(rightVisibility);

    }

    /**
     * 左边布局组件显示和隐藏
     *
     * @param resId
     *            图片资源ID
     * @param strText
     *            说明文字
     * @param imgVg
     *            true/false
     * @param textVg
     *            true/false
     */
    public void setLeftComponent(int resId, String strText, int imgVg,
                                 int textVg) {
        title_bar_left_imageview.setImageResource(resId);
        title_bar_left_textview.setText(strText);
        title_bar_left_imageview.setVisibility(imgVg);
        title_bar_left_textview.setVisibility(textVg);
    }

    /**
     * 中间布局组件显示和隐藏
     *
     * @param strText
     *            标题
     * @param textVg
     *            true/false
     */
    public void setCenterTitle(String strText, int textVg) {
        title_bar_title.setText(strText);
        title_bar_title.setVisibility(textVg);
    }

    /**
     * 右边布局组件显示和隐藏
     *
     * @param resId
     *            图片资源ID
     * @param strText
     *            说明文字
     * @param imgVg
     *            true/false
     * @param textVg
     *            true/false
     */
    public void setRightComponent(int resId, String strText, int imgVg,
                                  int textVg) {
        title_bar_right_iamgeview.setImageResource(resId);
        title_bar_right_textview.setText(strText);
        title_bar_right_iamgeview.setVisibility(imgVg);
        title_bar_right_textview.setVisibility(textVg);
    }

    /**
     * 左边布局点击事件响应
     *
     * @param listener
     *            回调
     */
    public void setBackOnClickListener(OnClickListener listener) {
        title_bar_layout_left.setOnClickListener(listener);
    }

    /**
     * 右边布局点击事件响应
     *
     * @param listener
     *            回调
     */
    public void setRightLayoutClickListener(OnClickListener listener) {
        title_bar_layout_right.setOnClickListener(listener);
    }

}
